/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.SecuredAction;
import com.ntb.controllers.forms.GroupListingForm;
import com.ntb.entities.UserGroups;
import com.ntb.models.UserGroupsDAO;
import com.ntb.models.impl.UserGroupsDAOImpl;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class GroupListingAction extends SecuredAction {

    @Override
    public ActionForward ntbExecute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        GroupListingForm groupListingForm = (GroupListingForm) form;
        UserGroupsDAO groupDao = new UserGroupsDAOImpl();        
        if (("delete").equalsIgnoreCase(groupListingForm.getAction())) {
            if (groupListingForm.getGroupID() == 1 || groupListingForm.getGroupID() == 2) {
                request.setAttribute("errorMessage", "Sorry, this group could not be deleted.");
            } else {
                if (groupDao.delete(groupListingForm.getGroupID()) == 0) {
                    request.setAttribute("errorMessage", "There was an error deleting this group. You must delete all users associated with this group before deleting the group.");
                } else {
                    request.setAttribute("successMessage", "The group was deleted successfully.");
                }
            }
        }

        Iterator<UserGroups> groupList = groupDao.getList();
        request.setAttribute("groupList", groupList);
        return mapping.getInputForward();
    }
}
